package com.pig4cloud.pig.module.controller.mer;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.Mer;
import com.pig4cloud.pig.module.api.entity.mer.MerPrinter;
import com.pig4cloud.pig.module.service.mer.IMerPrinterService;
import com.pig4cloud.pig.module.service.mer.IMerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 餐饮商家打印机列Controller
 *
 * @author ruoyi
 * @date 2023-05-05
 */
@Validated
@Api(value = "餐饮商家打印机列控制器", tags = {"餐饮商家打印机列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mer/printer")
public class MerPrinterController {

    private final IMerPrinterService merPrinterService;
    private final IMerService merService;
    /**
     * 查询餐饮商家打印机列列表
     */
    @ApiOperation("查询餐饮商家打印机列列表")
    @PreAuthorize("@pms.hasPermission('module:merPrinter:list')")
    @GetMapping("/list")
    public TableDataInfo<MerPrinter> list(@Validated MerPrinter merPrinter) {
        QueryWrapper<MerPrinter> qw = new QueryWrapper<>();
		if(merPrinter.getMerId()!= null){
			qw.eq("mer_id",merPrinter.getMerId());
		}
		if(merPrinter.getDeviceSn()!= null){
			qw.like("device_sn",merPrinter.getDeviceSn());
		}
        TableDataInfo<MerPrinter> td = PageUtils.buildDataInfo(merPrinterService.page(PageUtils.buildPage(),qw));
        List<MerPrinter> list = td.getData();
        for (MerPrinter list1:list){
            if (ObjectUtil.isNotEmpty(list1.getMerId())){
                Mer mer = merService.getById(list1.getMerId());
                list1.setMerName(mer.getShopName());
            }
        }
        return td;
    }

    /**
     * 获取餐饮商家打印机列详细信息
     */
    @ApiOperation("获取餐饮商家打印机列详细信息")
    @PreAuthorize("@pms.hasPermission('module:merPrinter:query')")
    @GetMapping("/{id}")
    public R<MerPrinter> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        MerPrinter merPrinter = merPrinterService.getById(id);
        return R.ok(merPrinter);
    }

    /**
     * 新增餐饮商家打印机列
     */
    @ApiOperation("新增餐饮商家打印机列")
    @PreAuthorize("@pms.hasPermission('module:merPrinter:add')")
    @SysLog("餐饮商家打印机列")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody MerPrinter merPrinter) {
        return R.okByRow(merPrinterService.save(merPrinter) ? 1 : 0);
    }

    /**
     * 修改餐饮商家打印机列
     */
    @ApiOperation("修改餐饮商家打印机列")
    @PreAuthorize("@pms.hasPermission('module:merPrinter:edit')")
    @SysLog("餐饮商家打印机列")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody MerPrinter merPrinter) {
        return R.okByRow(merPrinterService.updateById(merPrinter) ? 1 : 0);
    }

    /**
     * 删除餐饮商家打印机列
     */
    @ApiOperation("删除餐饮商家打印机列")
    @PreAuthorize("@pms.hasPermission('module:merPrinter:remove')")
    @SysLog("餐饮商家打印机列")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(merPrinterService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
